package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianji.TianjiLocationData;
import com.ucredit.babel.common.model.tianji.TianjiLocationRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 天机位置接口v1
 *
 * @author renzhanzheng
 */

public class TianjiLocationParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 天机位置接口v1
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TianjiLocationRecord obj = (TianjiLocationRecord)model;
        TianjiLocationData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject tianji = info.getJSONObject("tianji_api_agentgf_policymodel_response") == null ? new JSONObject() : info.getJSONObject("tianji_api_agentgf_policymodel_response");

        StringBuilder sb = getBaseInfo(obj, successful);
        //错误码
        sb.append(info.getString("error") == null ? "" : info.getString("error")).append(CRT001);
        //错误信息
        sb.append(info.getString("msg") == null ? "" : info.getString("msg")).append(CRT001);
        //请求ID
        sb.append(info.getString("request_id") == null ? "" : info.getString("request_id")).append(CRT001);
        //资产价值系数
        sb.append(tianji.getString("pr") == null ? "" : tianji.getString("pr")).append(CRT001);
        //定制评分结果
        sb.append(tianji.getString("cg") == null ? "" : tianji.getString("cg")).append(CRT001);
        //行为兴趣偏好
        sb.append(tianji.getString("in") == null ? "" : tianji.getString("in")).append(CRT001);
        //消费水平偏好
        sb.append(tianji.getString("co") == null ? "" : tianji.getString("co")).append(CRT001);
        //高位轨迹系数
        sb.append(tianji.getString("li") == null ? "" : tianji.getString("li")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tianji_locationv1_inc";
    }

    @Override
    public String getType() {
        return RecordType.TIANJI_LOCATION_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "天机位置接口v1";
    }
}
